


*================================
*SETTINGS
*================================
set more off
set trace off
set scheme s1mono
set linesize 255
set matsize 2000
cap log close _all

cap mkdir ./output/main-estimates

*---------------------------------*
*---------------------------------*
*OUTPUT DYNAMIC DISABILITY RESULTS FOR Figures 5 (panel b), 8 (panel b), A.9 (panels e and f), A.13, A.18 (panels c and d), A.19. (panel b), A.20 (panel c and d) , A.29 (panels d,e,f), A.30 (panels d,e,f).
*---------------------------------*
*---------------------------------*

foreach samp in  all black white A B { 

use "./processed/cleaned_analysis_data_NSC_VADC_v7.dta", clear

	foreach n in 31 50{
	gen inst`n'=(firstafqt>=`n')
	gen k`n'=firstafqt-`n'+.5
	gen instk`n'=inst`n'*k`n'	
	g tri`n' = 1-abs((firstafqt - `n'+.5)/19)
	g tri`n'_half = 1-abs((firstafqt - `n'+.5)/9)
	}

foreach x in k31 k50 instk31 instk50{
	gen `x'_2 = `x'^2
}

g yr = year(firstafqtdt)
g quarter = 1 if inrange(month(firstafqtdt),1,3)
replace quarter = 2 if inrange(month(firstafqtdt),4,6)
replace quarter = 3 if inrange(month(firstafqtdt),7,9)
replace quarter = 4 if inrange(month(firstafqtdt),10,12)
egen quarterFE = group(yr quarter)

global start = -1
global end = 19 


if "`samp'" == "black" keep if black == 1 
if "`samp'" == "white" keep if white == 1 
if "`samp'" == "A" keep if inrange(firstyear,1990,2000)
if "`samp'" == "B" keep if inrange(firstyear,2001,2011)
if "`samp'" == "B" global end = 17 


foreach outcome in  any_dis dis_tot any_worklim any_ssissdi ssissdi_tot { 
		cap mkdir "./output/main-estimates/`outcome'"
		if "`samp'" != "all" cap mkdir "./output/main-estimates/`outcome'/`samp'"
		if "`samp'" == "all" global output ./output/main-estimates/`outcome'/graphdata_`outcome'
		if "`samp'" != "all" global output ./output/main-estimates/`outcome'/`samp'/graphdata_`outcome'_`samp'

		*---- Initialize output file for graph data ----*
		preserve
			clear
			local obsnum = $end - $start + 1 
			set obs `obsnum'
			gen y = _n - 2 //- 3
			
				gen mean_cut31_2sls_quad = .
				gen mean_cut50_2sls_quad = .
				gen beta_cut31_2sls_quad = .
				gen beta_cut50_2sls_quad = .
				gen se_cut31_2sls_quad = .
				gen se_cut50_2sls_quad = .
				gen N_31_2sls_quad = .
				gen N_50_2sls_quad = .
				
		save "${output}", replace
		restore
		
		forval y=$start/$end { 
		
		local ytitle = `y'
			if `y' == -2 local ytitle m2
			if `y' == -1 local ytitle m1

			preserve
			
			keep `outcome'_`ytitle' k31 instk31 k31_2 instk31_2 quarterFE access inst31 firstafqt k50 instk50 k50_2 instk50_2 inst50 


			*---- 2sls regressions QUADRATIC ----*
			*2SLS 1st cutoff
			ivregress 2sls `outcome'_`ytitle' k31 instk31 k31_2 instk31_2 i.quarterFE (access=inst31) if inrange(firstafqt,12,49) , robust
				local N_1_2sls = e(N)
				summ `outcome'_`ytitle' if e(sample)
				local mean_cut1_2sls = r(mean)
				local coef_cut1_2sls = _b[access]
				local se_cut1_2sls = _se[access]
				
			*2SLS 2nd cutoff  
			 ivregress 2sls `outcome'_`ytitle' k50 instk50 k50_2 instk50_2 i.quarterFE (access=inst50) if inrange(firstafqt,31,68), robust
				local N_2_2sls = e(N)
				summ `outcome'_`ytitle' if e(sample)		
				local mean_cut2_2sls = r(mean)
				local coef_cut2_2sls = _b[access]
				local se_cut2_2sls = _se[access]
			
			*STORE COEFFICIENT ESTIMATES FOR A GRAPH 
			use "${output}", clear				
			foreach model in _2sls {
				replace mean_cut31_2sls_quad = `mean_cut1`model'' if y == `y'
				replace mean_cut50_2sls_quad = `mean_cut2`model'' if y == `y'
				replace beta_cut31_2sls_quad = `coef_cut1`model'' if y == `y'
				replace beta_cut50_2sls_quad = `coef_cut2`model'' if y == `y'
				replace se_cut31_2sls_quad = `se_cut1`model'' if y == `y'
				replace se_cut50_2sls_quad = `se_cut2`model'' if y == `y'
				replace N_31_2sls_quad = `N_1`model'' if y == `y'
				replace N_50_2sls_quad = `N_2`model'' if y == `y'
			}
			save  "${output}", replace
			restore
			
		} // end loop over relative years
} // end outcome loop
} // end sample loop


*---------------------------------*
*---------------------------------*
*OUTPUT CF DISABILITY (A.18 c and d)
*---------------------------------*
*---------------------------------*
*LOAD CLEANED DATASET

foreach samp in black white { 

use "./processed/cleaned_analysis_data_NSC_VADC_v7.dta", clear


*CONSTRUCT RUNNING VARIABLE AND INSTRUMENT
	foreach n in 31 50{
	gen inst`n'=(firstafqt>=`n')
	gen k`n'=firstafqt-`n'+.5
	gen instk`n'=inst`n'*k`n'	
	g tri`n' = 1-abs((firstafqt - `n'+.5)/19)
	g tri`n'_half = 1-abs((firstafqt - `n'+.5)/9)
	}

foreach x in k31 k50 instk31 instk50{
	gen `x'_2 = `x'^2
}

*CONSTRUCT FIRSTAFQT QUARTER
g yr = year(firstafqtdt)
g quarter = 1 if inrange(month(firstafqtdt),1,3)
replace quarter = 2 if inrange(month(firstafqtdt),4,6)
replace quarter = 3 if inrange(month(firstafqtdt),7,9)
replace quarter = 4 if inrange(month(firstafqtdt),10,12)
egen quarterFE = group(yr quarter)

global start = -1
global end = 19 


if "`samp'" == "black" keep if black == 1 
if "`samp'" == "white" keep if white == 1 
if "`samp'" == "male" keep if male == 1
if "`samp'" == "female" keep if male == 0
if "`samp'" == "A" keep if inrange(firstyear,1990,2000)
if "`samp'" == "B" keep if inrange(firstyear,2001,2011)
if "`samp'" == "B" global end = 17 


foreach outcome in  cf_any_dis  { 
		cap mkdir "./output/main-estimates/`outcome'"
		if "`samp'" != "all" cap mkdir "./output/main-estimates/`outcome'/`samp'"
		if "`samp'" == "all" global output ./output/main-estimates/`outcome'/graphdata_`outcome'
		if "`samp'" != "all" global output ./output/main-estimates/`outcome'/`samp'/graphdata_`outcome'_`samp'

		*---- Initialize output file for graph data ----*
		preserve
			clear
			local obsnum = $end - $start + 1 
			set obs `obsnum'
			gen y = _n - 2 //- 3
			
				gen mean_cut31_2sls_quad = .
				gen mean_cut50_2sls_quad = .
				gen beta_cut31_2sls_quad = .
				gen beta_cut50_2sls_quad = .
				gen se_cut31_2sls_quad = .
				gen se_cut50_2sls_quad = .
				gen N_31_2sls_quad = .
				gen N_50_2sls_quad = .
				
		save "${output}", replace
		restore
		
		*-------------------------------------------------------*
		*---- Reduced-form for years relative to first AFQT ----*
		*-------------------------------------------------------*
		forval y=$start/$end { // loop through years pre/post afqt date year
		
		local ytitle = `y'
			if `y' == -2 local ytitle m2
			if `y' == -1 local ytitle m1

			cap drop cf_any_dis_`ytitle' 
			cap g cf_any_dis_`ytitle' = -any_dis_`ytitle'*(1-access)

			cap drop cf_any_ssissdi_`ytitle' 
			cap g cf_any_ssissdi_`ytitle' = -any_ssissdi_`ytitle'*(1-access)
			
			cap drop cf_dis_tot_`ytitle' 
			cap g cf_dis_tot_`ytitle' = -dis_tot_`ytitle'*(1-access)

			preserve
			
			keep `outcome'_`ytitle' k31 instk31 k31_2 instk31_2 quarterFE access inst31 firstafqt k50 instk50 k50_2 instk50_2 inst50 


			
			*---- 2sls regressions QUADRATIC ----*
			*2SLS 1st cutoff
			ivregress 2sls `outcome'_`ytitle' k31 instk31 k31_2 instk31_2 i.quarterFE (access=inst31) if inrange(firstafqt,12,49) , robust
				local N_1_2sls = e(N)
				summ `outcome'_`ytitle' if e(sample)
				local mean_cut1_2sls = r(mean)
				local coef_cut1_2sls = _b[access]
				local se_cut1_2sls = _se[access]
				
			*2SLS 2nd cutoff  
			 ivregress 2sls `outcome'_`ytitle' k50 instk50 k50_2 instk50_2 i.quarterFE (access=inst50) if inrange(firstafqt,31,68), robust
				local N_2_2sls = e(N)
				summ `outcome'_`ytitle' if e(sample)		
				local mean_cut2_2sls = r(mean)
				local coef_cut2_2sls = _b[access]
				local se_cut2_2sls = _se[access]
			
			*STORE COEFFICIENT ESTIMATES FOR A GRAPH 
			use "${output}", clear				
			foreach model in _2sls {
				replace mean_cut31_2sls_quad = `mean_cut1`model'' if y == `y'
				replace mean_cut50_2sls_quad = `mean_cut2`model'' if y == `y'
				replace beta_cut31_2sls_quad = `coef_cut1`model'' if y == `y'
				replace beta_cut50_2sls_quad = `coef_cut2`model'' if y == `y'
				replace se_cut31_2sls_quad = `se_cut1`model'' if y == `y'
				replace se_cut50_2sls_quad = `se_cut2`model'' if y == `y'
				replace N_31_2sls_quad = `N_1`model'' if y == `y'
				replace N_50_2sls_quad = `N_2`model'' if y == `y'
			}
			save  "${output}", replace
			restore
			
		} // end loop over relative years
} // end outcome loop
} // end sample loop

